<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <div class="table-operator">
      <div class="row-item-tab clearfix">
        <a-button @click="handleSet">设置提现账户</a-button>
      </div>
    </div>
    <s-table
      ref="table"
      rowKey="id"
      :loading="isLoading"
      :columns="columns"
      :data="loadData"
      :pageSize="15"
    >
      <span slot="certificate" slot-scope="text">
        <a title="点击查看原图" :href="host + '/uploads/' + text" target="_blank">
          <img v-if="text" width="45" height="45" :src="host + '/uploads/' + text" alt="用户头像" />
          <img v-else width="45" height="45" src="~@/assets/img/default-avatar.png" alt="用户头像" />
        </a>
      </span>
      <!-- 操作 -->
      <span class="actions" slot="action" slot-scope="item">
        <a v-if="item.state == 0" title="申请" @click="handleApply(item)">申请</a>
      </span>
    </s-table>
    <Setting ref='SettingModel' @handleSubmit='handleRefresh' />
  </a-card>
</template>

<script>
import * as Api from '@/api/user'
import { STable } from '@/components'
import Setting from './Setting'
import moment from 'moment'

export default {
  name: 'Index',
  components: {
    STable,
    Setting
  },
  data () {
    return {
      host: window.publicConfig.HOST,
      searchForm: this.$form.createForm(this),
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      // 表头
      columns: [
        {
          title: '商家名称',
          dataIndex: 'store_name'
        },
        {
          title: '当前余额',
          dataIndex: 'balance'
        },
        {
          title: '状态',
          dataIndex: 'state',
          customRender: (value) => {
            let response = ''
            switch (value) {
              case 0:
                response = '待领取'
                break

              case 1:
                response = '申请中'
                break

              case 2:
                response = '已发放'
                break

              case 3:
                response = '拒绝'
                break
            }
            return response
          }
        },
        {
          title: '发放金额',
          dataIndex: 'money'
        },
        // {
        //   title: '触发时间',
        //   dataIndex: 'create_at',
        //   customRender: (value) => {
        //     if (!value) return '-'
        //     return moment(value * 1000).format('YYYY/MM/DD HH:mm:ss')
        //   }
        // },
        {
          title: '申请时间',
          dataIndex: 'apply_at',
          customRender: (value) => {
            if (!value) return '-'
            return moment(value * 1000).format('YYYY/MM/DD HH:mm:ss')
          }
        },
        {
          title: '审批时间',
          dataIndex: 'take_at',
          customRender: (value) => {
            if (!value) return '-'
            return moment(value * 1000).format('YYYY/MM/DD HH:mm:ss')
          }
        },
        {
          title: '凭证',
          dataIndex: 'certificate',
          scopedSlots: { customRender: 'certificate' }
        },
        {
          title: '操作',
          // dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: param => {
        return Api.clist({ ...param, ...this.queryParam })
          .then(response => {
            return response.data.list
          })
      }
    }
  },
  created () {
  },
  methods: {
    handleSet() {
      this.$refs.SettingModel.add()
    },
    handleApply (item) {
      const that = this
      this.$confirm({
        title: '确认提示',
        content: `确认申请额度？`,
        okType: 'danger',
        onOk () {
          Api.apply({ id: item.id })
            .then(response => {
              that.$message.success(response.message)
              that.handleRefresh(true)
            })
        }
      })
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh (bool = false) {
      this.$refs.table.refresh(bool)
    }

  }
}
</script>

<style lang="less" scoped>
.ant-card-body {
  padding: 22px 29px 25px;
}

.platform {
  .icon {
    font-size: 17px;
  }

  .MP-WEIXIN {
    color: #04be02;
  }

  .H5 {
    color: #e44c27;
  }
}
</style>
